﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic.CompilerServices;

namespace FMS.Formulation.Licensing.Engine
{
    public class CheckCode
    {
        public string code(string code)
        {

            if (code.Length != 26)
		{
                return null;
            }
            StringBuilder stringBuilder = new StringBuilder();
            int num = 0;
            checked
            {
                do
                {
                    stringBuilder.Append(code.Substring(num * 5, 5));
                    stringBuilder.Append('-');
                    num++;
                }
                while (num <= 4);
                stringBuilder.Append(code.Substring(25));
                string text = stringBuilder.ToString();
                CheckCode2 check2 = new CheckCode2();
                if (check2 != null && !check2.VerifyInstallationCode(text))
			{
                    return null;
                }
                return text;
            }
        }
    }
    internal class CheckCode2
    {

        public bool result;
        public CheckCode2()
        {

        }
        public bool VerifyInstallationCode(string code)
        {
            return check2s(code);
        }

        private static char[] k1o = new char[32]
{
    '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
    'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
    'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z'
};
        private bool check2s(string code)
        {


            bool rs = false;
            if (code == null || code.Length != 31)
            {
                return rs; 
            }
            BitArray bitArray = new BitArray(128);
            byte[] array = new byte[16];
            int num = 0;
            checked
            {
                int num2 = default(int);
                int num5 = default(int);
                do
                {
                    if (num2 == 5)
                    {
                        if (Operators.CompareString(Conversions.ToString(code[num]), "-", false) != 0)
                        {
                            return rs;
                        }
                        num2 = 0;
                    }
                    else
                    {
                        int num3 = Array.IndexOf(k1o, code[num]);
                        if (num3 < 0)
                        {
                            return rs;
                        }
                        BitArray bitArray2 = new BitArray(new byte[1] { Convert.ToByte(num3) });
                        int num4 = 0;
                        do
                        {
                            bitArray[num5] = bitArray2[num4];
                            num5++;
                            if (num5 == 128)
                            {
                                for (int i = num4 + 1; i <= 4; i++)
                                {
                                    if (bitArray2[i])
                                    {
                                        return rs;
                                    }
                                }
                                break;
                            }
                            num4++;
                        }
                        while (num4 <= 4);
                        num2++;
                    }
                    num++;
                }
                while (num <= 30);
                bitArray.CopyTo(array, 0);
                G5G(array, code);
            }
            return result;
        }

        private void G5G(byte[] arr, string code)
        {
            R2r r = new R2r();
            try
            {
                
                byte  [] arr2 = r.c1c(code  );

                byte[] s2 = r.q1(arr, arr2);

                result = k4k(s2);

            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                //;` = false;
                ProjectData.ClearProjectError();
            }
        }


        private bool k4k(byte[] code)
        {
            bool rs = false;

            if (code.Length != 15)
            {
               
                return rs;
            }
            byte[] array = new byte[16];
            byte b;
            checked
            {
                b = (byte)unchecked((uint)code[0] / (uint)Convert.ToByte(16));
            }
            byte b2 = (byte)(code[0] & Convert.ToByte(15));
            if ((uint)b >= (uint)b2 || b2 == 0)
            {
                rs = false;
                return rs;
            }
            int num = 1;
            int num2 = 0;
            checked
            {
                do
                {
                    if (num2 != b && num2 != b2)
                    {
                        array[num2] = code[num];
                        num++;
                    }
                    num2++;
                }
                while (num2 <= 15);
                if (array[0] == (array[4] ^ array[8] ^ array[12]) && array[1] == (array[5] ^ array[9] ^ array[13]) && array[2] == (array[6] ^ array[10] ^ array[14]) && array[3] == (array[7] ^ array[11] ^ array[15]))
                {
                    Guid guid = new Guid(array);
                    rs = true;
                }
            }
            return false;
        }

    }
    internal class R2r
    {
        private static PermissionSet a0()
{
	PermissionSet permissionSet = new PermissionSet(PermissionState.None);
        permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.SerializationFormatter));
	return permissionSet;
}
        
        public static bool At1()
        {
            bool z2z = false;
            bool i2I = false;
            bool f2f = false;
            if (z2z)
		{
                return i2I;
            }
            try
            {
                RijndaelManaged rijndaelManaged = new RijndaelManaged();
                //3 >.; q("Succesfully created RijndaelManaged object in UseFIPS", TraceLevel.Info);
            }
            catch (InvalidOperationException ex)
            {
                ProjectData.SetProjectError(ex);
                InvalidOperationException ex2 = ex;
                i2I = true;
                //3 >.; q("m_UseFIPS set to True after error ", ex2, TraceLevel.Info);
                try
                {
                    SHA256CryptoServiceProvider sHA256CryptoServiceProvider = new SHA256CryptoServiceProvider();
                }
                catch (PlatformNotSupportedException ex3)
                {
                    ProjectData.SetProjectError(ex3);
                    PlatformNotSupportedException ex4 = ex3;
                    //3 >.; q("m_Usesi256 set to True after error ", ex4, TraceLevel.Info);
                    f2f = true;
                    ProjectData.ClearProjectError();
                }
                ProjectData.ClearProjectError();
            }
            finally
            {
                z2z = true;
            }
            return i2I;
        }
        

        static byte[] E1E(string code)
{
            byte[] hash;

    if (At1())
	{
                SHA1CryptoServiceProvider sHA1CryptoServiceProvider = new SHA1CryptoServiceProvider();
                sHA1CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes((code)));
                hash = sHA1CryptoServiceProvider.Hash;
                sHA1CryptoServiceProvider.Clear();
                return hash;
            }
	SHA1Managed sHA1Managed = new SHA1Managed();

    sHA1Managed.ComputeHash(Encoding.UTF8.GetBytes((code)));
	hash = sHA1Managed.Hash;
    sHA1Managed.Clear();
	return hash;
}


    public byte[] c1c(string code)
        {
            byte[] array = new byte[16];
            byte[] sourceArray = E1E(code);
            Array.Copy(sourceArray, 2, array, 0, 16);
            return array;
        }

        public byte[] q1(byte[] code, byte[] code2)
{
            return null;


            //if (At1())
            //{
            //    return 0:(1m, 1m, 1m);
            //}
            //RijndaelManaged rijndaelManaged = null;
            //try
            //{

            //    rijndaelManaged = new RijndaelManaged();
            //    return 51(rijndaelManaged, 1m, 1m, 1m);
            //}
            //catch (Exception ex)
            //{
            //    ProjectData.SetProjectError(ex);
            //    Exception ex2 = ex;
            //    throw ex2;
            //}
            //finally
            //{
            //    if (rijndaelManaged != null)
            //    {
            //        rijndaelManaged.Clear();
            //    }
            //}
        }

    }
}
